package com.allwees.bs.core.modelbase.vo;

import com.allwees.bs.core.modelbase.constant.ResultEnum;
import com.allwees.core.common.i18n.MessageSourceUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.apache.http.HttpStatus;

import java.util.HashMap;


/**
 * 返回数据
 *
 * @author yangylsky
 */
public class R extends HashMap<String, Object> {
    private static final long serialVersionUID = 1L;

    public static final int CODE_OK = 1;

    public static R ok() {
        return new R()
                .put("code", CODE_OK)
                .put("msg", MessageSourceUtil.getMessage(ResultEnum.SUCCESS));
    }


    public static R ok(String msg) {
        return ok().put("msg",
                MessageSourceUtil.getMessageOrDefault(msg, msg));
    }

    public static R ok(String msg, Object data) {
        return ok().put("msg", MessageSourceUtil.getMessageOrDefault(msg, msg))
                .put("data", data);
    }

    public static R ok(Object data) {
//        R r = ok().put("data", data);
//        if (data instanceof Collection<?>) {
//            int total = ((Collection<?>) data).size();
//            r.put("total", total);
//            r.put("count", total);
//        }
        return ok().put("data", data);
    }

    public static <T> R ok(IPage<T> page) {
        int count = page.getRecords() == null ? 0 : page.getRecords().size();
        long pageSize = page.getSize();
        long total = page.getTotal();
        long current = page.getCurrent();
        R r = ok()
                .put("data", page.getRecords())
                .put("total", total)
                .put("count", count)
                .put("current", current)
                .put("size", pageSize);

        long lastPage = (total + pageSize - 1) / pageSize;
        r.put("lastPage", lastPage);
        r.put("hasMorePages", lastPage > current);
        return r;
    }

    public static R error(ResultEnum e) {
        String message = MessageSourceUtil.getMessageOrDefault(e, MessageSourceUtil.getMessage("system.error"));
        return new R()
                .put("code", e.getCode())
                .put("msg", message);
    }

    public static R error(int code, Enum e) {
        String message = MessageSourceUtil.getMessageOrDefault(e, MessageSourceUtil.getMessage("system.error"));
        return new R()
                .put("code", code)
                .put("msg", message);
    }

    public static R error(int code, String msg) {
        String message = MessageSourceUtil.getMessageOrDefault(msg, msg);
        return new R()
                .put("code", code)
                .put("msg", message);
    }

    public static R error(int code, String msg, Object[] args) {
        String message = MessageSourceUtil.getMessageOrDefault(msg, msg, args);
        return new R()
                .put("code", code)
                .put("msg", message);
    }

    public static R error(String msg) {
        return error(HttpStatus.SC_INTERNAL_SERVER_ERROR, msg);
    }

    @Override
    public R put(String key, Object value) {
        super.put(key, value);
        return this;
    }
}
